package com.homevip.controller.platform.user;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.homevip.core.controller.BaseController;
import com.homevip.core.page.Page;
import com.homevip.core.page.PageRequest;
import com.homevip.core.page.Sort;
import com.homevip.user.entity.User;
import com.homevip.user.entity.User_role;
import com.homevip.user.service.RoleService;
import com.homevip.user.service.UserService;
import com.homevip.util.StringUtil;
import com.homevip.util.gson.JsonUtil;
import com.homevip.util.system.CommArray;
import com.homevip.util.system.Const;
import com.homevip.web.annotation.DataTypeAnnotation;
import com.homevip.web.annotation.DataTypeEnum;
import com.homevip.web.vo.ResultVo;

/**
 * 工人页面
 * 
 * @author panpan
 */
@Controller
@RequestMapping("/platform/user/worker/*")
public class PlatformUserWorkerController extends BaseController {
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;

	/**
	 * 用户列表
	 */
	@RequestMapping("/list")
	public void list(String keyword, Integer status, Integer pageno, HttpServletRequest request) {
		PageRequest pr = new PageRequest(null == pageno ? 0 : pageno, Const.DEFINE_PAGE);
		pr.setSort(new Sort("id", false));
		Page<User> page = userService.findUser(keyword, CommArray.EnumStaffType._1.getCode(), status, pr);
		request.setAttribute("page", page);
	}

	/**
	 * 用户编辑
	 */
	@RequestMapping("/edit")
	public void edit(Integer id, HttpServletRequest request) {
		if (null != id && id > 0) {
			User item = userService.getUserById(id);
			
			List<Integer> roleids = new ArrayList<Integer>();
			if(null != item.getRoles()){
				for(User_role ur  : item.getRoles()){
					roleids.add(ur.getId());
				}
			}
			request.setAttribute("roleids", StringUtil.list2Str(roleids));
			request.setAttribute("item", item);
		}

		request.setAttribute("roles", roleService.findUserRole(null, 2));
	}

	/**
	 * 用户编辑保存
	 */
	@RequestMapping("/ajax_edit")
	@DataTypeAnnotation(DataTypeEnum.json)
	public void ajaxEdit(User user, Integer nationid, Integer[] roleids, Integer[] groupids, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultVo resultVo = new ResultVo();
		try {
			user.setRoles(new ArrayList<User_role>());
			if(null != roleids){
				for(int roleid : roleids){
					User_role role = new User_role();
					role.setId(roleid);
					user.getRoles().add(role);
				}
			}

			user.setUsertype(CommArray.EnumStaffType._1.getCode());
			resultVo = userService.saveUser(user);
		} catch (Exception e) {
			resultVo = new ResultVo(false, "编辑失败");
		}
		writeJson(response, JsonUtil.toJson(resultVo));
	}

	/**
	 * 用户编辑删除
	 * 
	 * @param _action_id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("ajax_delete")
	@DataTypeAnnotation(DataTypeEnum.json)
	public void ajaxDelete(Integer _action_id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultVo resultVo = new ResultVo();
		User user = new User();
		if (null != _action_id && _action_id > 0) {
			user.setId(_action_id);
			try {
				userService.deleteUser(user);
			} catch (Exception e) {
				resultVo = new ResultVo(false, "删除失败");
			}
		}
		writeJson(response, JsonUtil.toJson(resultVo));
	}

	/**
	 * 角色更新状态
	 * 
	 * @param _action_id
	 * @param request
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("/ajax_updatestatus")
	@DataTypeAnnotation(DataTypeEnum.json)
	public void ajaxUpdateStatus(Integer _action_id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultVo resultVo = new ResultVo();
		User user;
		if (null != _action_id && _action_id > 0) {
			user = userService.getUserById(_action_id);
			try {
				user.setStatus(user.getStatus() == 1 ? 2 : 1);
				userService.saveUser(user);
			} catch (Exception e) {
				resultVo = new ResultVo(false, "更新失败");
			}
		}
		writeJson(response, JsonUtil.toJson(resultVo));
	}

	/**
	 * 用户密码修改
	 */
	@RequestMapping("/changepass")
	@DataTypeAnnotation(DataTypeEnum.json)
	public void changePass(Integer id, HttpServletRequest request) throws Exception {
	}

	/**
	 * 用户密码修改处理
	 */
	@RequestMapping("/ajax_changepass")
	@DataTypeAnnotation(DataTypeEnum.json)
	public void ajaxChangePass(Integer id, String password,HttpServletRequest request, HttpServletResponse response) throws Exception {
		ResultVo resultVo = new ResultVo();
		User user;
		if (null != id && id > 0) {
			user = userService.getUserById(id);
			try {
				user.setPassword(password);
				userService.saveUserPass(user);
			} catch (Exception e) {
				resultVo = new ResultVo(false, "更新失败");
			}
		}
		writeJson(response, JsonUtil.toJson(resultVo));
	}
}
